package com.edunera;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.edunera.connection.Connection;
import com.edunera.connection.TaskIsConnectedBefore;
import com.edunera.connection.TaskLoginToEdunera;

public class ActivityLogin  extends Activity implements OnClickListener{
    final Context context=this;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String android_id = Secure.getString(getBaseContext().getContentResolver(),Secure.ANDROID_ID);

        Log.d("Device Id", android_id);

        setContentView(R.layout.login);

        Button b=(Button)findViewById(R.id.btn_login);
        b.setOnClickListener(this);
        if(android_id!=null && !android_id.equals("")){
            new TaskIsConnectedBefore(context, android_id) {
                @Override
                public void onComplete() {
                    if(isConnected) {
                        goToMainScreen();
                    }
                }
            }.execute();
        }
    }

    public void onClick(View v){

        EditText password= (EditText)findViewById(R.id.login_text_password);
        EditText email= (EditText)findViewById(R.id.login_email);
        String android_id = Secure.getString(getBaseContext().getContentResolver(),Secure.ANDROID_ID);
        if(email!=null && password!=null)
        {
            new TaskLoginToEdunera(context,android_id,email.getText().toString(),password.getText().toString()) {
            @Override
            public void onComplete() {
                if(status) {
                    goToMainScreen();
                }else
                {
                    showAlert("Login","Login is unsuccessfull, please try again",true);
                }
            }
            }.execute();


        }else
        {

            showAlert("Login","Login is unsuccessfull, please try again",true);
        }

    }

    private void goToMainScreen(){
        Intent i=new Intent(this,ActivityMain.class);
        startActivity(i);
    }

    private void showAlert(String title, String message,boolean hasButtons){
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title); //Set Alert dialog title here
        alert.setMessage(message); //Message here
        if(hasButtons){
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();

                }
            });
        }
        AlertDialog alertDialog = alert.create();
        alertDialog.show();
    }

}
